package com.lzy.imagepickerdemo.zn;

import android.Manifest;
import android.content.Context;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v4.app.ActivityCompat;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.lzy.imagepicker.ImageDataSource;
import com.lzy.imagepicker.ImagePicker;
import com.lzy.imagepicker.bean.ImageFolder;
import com.lzy.imagepicker.ui.ImageBaseActivity;
import com.lzy.imagepickerdemo.GApp;
import com.lzy.imagepickerdemo.R;
import com.lzy.imagepickerdemo.imageloader.GlideImageLoader;
import com.lzy.imagepickerdemo.zn.adapter.PersonalTypeAdapter;
import com.lzy.imagepickerdemo.zn.db.PhotoTypeEntity;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class SearchActivity extends ImageBaseActivity implements ImageDataSource.OnImagesLoadedListener{

    @Bind(R.id.ed_content)
    EditText edContent;
    @Bind(R.id.btn_serache)
    Button btnSerache;
    @Bind(R.id.rev_photo_list)
    RecyclerView revPhotoList;
    ImagePicker imagePicker;
    private static final int REQUEST_PERMISSION_STORAGE = 1001;
    List<ImageFolder> imageFolders=new ArrayList<>();
    PersonalTypeAdapter mPersonalTypeAdapter;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_search);
        ButterKnife.bind(this);
        imagePicker = ImagePicker.getInstance();
        imagePicker.setImageLoader(new GlideImageLoader());
        mPersonalTypeAdapter=new PersonalTypeAdapter();
        revPhotoList.setLayoutManager(new LinearLayoutManager(this));
        revPhotoList.setAdapter(mPersonalTypeAdapter);
        ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, REQUEST_PERMISSION_STORAGE);
    }

    @OnClick(R.id.btn_serache)
    public void onViewClicked() {
        String ed=edContent.getText().toString().trim();
        /*List<ImageItem> mlist=new ArrayList<>();
        List<ImageItem> mlist2=new ArrayList<>();
        for (ImageFolder im:imageFolders){
            mlist2.addAll(im.images);
        }
            for (ImageItem mImageItem:mlist2){
                if (mImageItem.name.contains(ed)){
                    mlist.add(mImageItem);
                }
            }
        Log.i("程序", "搜索->>>>" + mlist.size()+"--》》"+mlist2.size());
        mPersonalTypeAdapter.setNewData(mlist);*/
        mPersonalTypeAdapter.setNewData(GApp.getInstance().mDBManager.getPhotoType(ed));
        mPersonalTypeAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                PhotoTypeEntity mPhotoTypeEntity= mPersonalTypeAdapter.getData().get(position);
                PhotoListActivity.start(SearchActivity.this,mPhotoTypeEntity.photo_type,mPhotoTypeEntity.name);
            }
        });
        InputMethodManager inputMethodManager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
        inputMethodManager.hideSoftInputFromWindow(edContent.getWindowToken(), 0);
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        if (requestCode == REQUEST_PERMISSION_STORAGE) {
            if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                new ImageDataSource(SearchActivity.this, null, this);
            } else {
               showToast("权限被禁止，无法选择本地图片");
            }
        }
    }
    @Override
    public void onImagesLoaded(List<ImageFolder> mimageFolders) {
        Log.i("程序", "程序返回数据->>>>" + imageFolders.size());
        //initViews(imageFolders);
        imageFolders.addAll(mimageFolders);

    }
}
